package main.Q201_300;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Q281_290 {
    public static void main(String[] args) {
        System.out.println("Question281：");
        System.out.println("Question282：");
        System.out.println("Question283：移动零");
        System.out.println("Question284：顶端迭代器");
        System.out.println("Question285：");
        System.out.println("Question286：");
        System.out.println("Question287：寻找重复数");
        System.out.println("Question288：");
        System.out.println("Question289：生命游戏");
        System.out.println("Question290：单词规律");
    }
}

class Question283{
//    public void moveZeroes(int[] nums) {
//        int zeroP=0,numP= 1,temp=0;//指向0的指针，指向非0的指针
//        if (nums.length==1) return;
//        while (numP< nums.length&&zeroP<numP){
//            if (nums[zeroP]==0&&nums[numP]!=0){//满足条件进行交换
//                temp=nums[zeroP];
//                nums[zeroP]=nums[numP];
//                nums[numP]=temp;
//                zeroP++;
//                numP++;
//            }else if (nums[zeroP]==0&&nums[numP]==0){//不满足交换条件，移动指针
//                numP++;
//            }else if (nums[zeroP]!=0&&nums[numP]!=0){
//                zeroP++;
//                numP++;
//            }else {
//                zeroP++;
//                numP++;
//            }
//        }
//    }

    public void moveZeroes(int[] nums) {
        int left=0,right=0;
        while(right<nums.length) {
            if(nums[right]==0) {// 等于0跨越
                right++;
            }else {// 不等于0覆盖
                nums[left]=nums[right];
                left++;
                right++;
            }
        }
        while(left<nums.length) {// 剩余全为0
            nums[left++]=0;
        }
    }
}

class Question284 implements Iterator<Integer> {
    Iterator<Integer> iterator;
    int nextInteger=-1;
    public Question284(Iterator<Integer> iterator) {
        // initialize any member here.
        this.iterator=iterator;
        nextInteger= iterator.next();
    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
        return nextInteger;
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        int result=nextInteger;
        if (iterator.hasNext()) nextInteger= iterator.next();
        else nextInteger=-1;
        return result;
    }

    @Override
    public boolean hasNext() {
        return nextInteger!=-1;
    }
}

class Question287{
    public static int findDuplicate(int[] nums) {//将数组中index与值一一对应
        int index=0,out=nums[index];
        while (true){
            if (nums[out]==out) break;
            index=nums[out];//该数离开数组
            nums[out]=out;
            out=index;
        }
        return out;
    }
}

class Question289{
    public void gameOfLife(int[][] board) {
        Map<int[],Integer> changeLifeState=new HashMap<>();
        int row= board.length,col=board[0].length;
        for (int i=0;i< row;i++){
            for (int j=0;j< col;j++){
                int count=0;
                int[] point={i,j};
                //检查周围细胞清空
                count=checkLiveCount(board,i,j);
                if (count<2&&board[i][j]==1){//少于2个且当前为活细胞，改变生命状态
                    changeLifeState.put(point,0);
                }else if (count>3&&board[i][j]==1){//大于3个且当前为活细胞，改变生命状态
                    changeLifeState.put(point,0);
                }else if (count==3&&board[i][j]==0){//等于3且为死细胞，改变生命状态
                    changeLifeState.put(point,1);
                }
            }
        }
        for (int[] point:changeLifeState.keySet()){
            board[point[0]][point[1]]=changeLifeState.get(point);
        }
    }
    public int checkLiveCount(int[][] board,int i,int j){
        int count=0,row= board.length,col=board[0].length;
        if (i-1>=0&&j-1>=0&&board[i-1][j-1]==1) count++;//左上
        if (i-1>=0&&board[i-1][j]==1) count++;//正上
        if (i-1>=0&&j+1<col&&board[i-1][j+1]==1) count++;//右上
        if (j-1>=0&&board[i][j-1]==1) count++;//正左
        if (j+1<col&&board[i][j+1]==1) count++;//正右
        if (i+1<row&&j-1>=0&&board[i+1][j-1]==1) count++;//左下
        if (i+1<row&&board[i+1][j]==1) count++;//正下
        if (i+1<row&&j+1<col&&board[i+1][j+1]==1) count++;//右下
        return count;
    }
}

class Question290{
    public static boolean wordPattern(String pattern, String s) {//map的双向映射
        Map<String, Character> str2ch = new HashMap<String, Character>();
        Map<Character, String> ch2str = new HashMap<Character, String>();
        int m = s.length();
        int i = 0;
        for (int p = 0; p < pattern.length(); ++p) {
            char ch = pattern.charAt(p);
            if (i >= m) {
                return false;
            }
            int j = i;
            while (j < m && s.charAt(j) != ' ') {
                j++;
            }
            String tmp = s.substring(i, j);
            if (str2ch.containsKey(tmp) && str2ch.get(tmp) != ch) {
                return false;
            }
            if (ch2str.containsKey(ch) && !tmp.equals(ch2str.get(ch))) {
                return false;
            }
            str2ch.put(tmp, ch);
            ch2str.put(ch, tmp);
            i = j + 1;
        }
        return i >= m;
    }
}